package my.diplom.web.controllers;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.validation.Errors;
import org.springframework.validation.BindException;
import my.diplom.dao.service.MessageService;
import my.diplom.dao.service.StatusService;
import my.diplom.dao.service.UserService;
import my.diplom.entity.User;
import my.diplom.entity.Message;
import my.diplom.entity.Status;
import my.diplom.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.beans.PropertyEditorSupport;

/**
 * Created by IntelliJ IDEA.
 * User: GGobozov
 * Date: 21.08.2009
 * Time: 15:56:02
 * To change this template use File | Settings | File Templates.
 */
public class MessageController extends SimpleFormController {

    private MessageService messageService;
    private StatusService statusService;
    private UserService userService;

    public MessageController() {
        setCommandClass(Message.class);
        setCommandName("newMessage");
        setSessionForm(false);
        setBindOnNewForm(false);
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void setStatusService(StatusService statusService) {
        this.statusService = statusService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @Override
    protected Map referenceData(HttpServletRequest request, Object o, Errors errors) throws Exception {
        HashMap<String, Object> model = new HashMap<String, Object>();
        User user = userService.getByLogin(UserUtil.getRemoteUser());
        List<Message> receivedMessages = messageService.getReceivedByUser(user);
        List<Message> sentMessages = messageService.getSentByUser(user);
        List<User> users = userService.getAll();
        model.put("users", users);
        model.put("receivedMessages", receivedMessages);
        model.put("sentMessages", sentMessages);
        return model;
    }


    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, BindException e) throws Exception {
        Message message = (Message)o;
        message.setFromUser(userService.getByLogin(UserUtil.getRemoteUser()));
        message.setDate(new Date());
        message.setStatus(statusService.getByName("unread"));
        messageService.create(message);
        return new ModelAndView(new RedirectView("createMessage.htm"));
    }

     @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
          binder.registerCustomEditor(User.class, "toUser", new PropertyEditorSupport() {

            public void setAsText(String text) {
                if (text instanceof String) {
                   Long userId = Long.parseLong(text);
                   User toUser = userService.getById(userId);
                    setValue(toUser);

                }
            }



        });

     }
}
